package com.pxl.springbootinit.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pxl.springbootinit.common.BaseResponse;
import com.pxl.springbootinit.common.ErrorCode;
import com.pxl.springbootinit.common.ResultUtils;
import com.pxl.springbootinit.exception.BusinessException;
import com.pxl.springbootinit.model.dto.task.TaskClaimForm;
import com.pxl.springbootinit.model.dto.task.TaskForm;
import com.pxl.springbootinit.model.entity.User;
import com.pxl.springbootinit.model.vo.TaskClaimVO;
import com.pxl.springbootinit.model.vo.TaskVO;
import com.pxl.springbootinit.service.TaskClaimService;
import com.pxl.springbootinit.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * 任务认领接口类
 *
 * @author MADAO
 * @create 2024 - 12 - 21 15:24
 */
@Slf4j
@RestController
@RequestMapping("/task/claim")
public class TaskClaimController {

    @Resource
    private TaskClaimService taskClaimService;
    @Resource
    private UserService userService;

    /**
     * 任务认领
     * @param taskClaimForm
     * @return
     */
    @PostMapping
    public BaseResponse<Boolean> claimTask(@RequestBody TaskClaimForm taskClaimForm, HttpServletRequest request) {
        User loginUser = userService.getLoginUser(request);
        if (loginUser == null){
            throw new BusinessException(ErrorCode.NOT_LOGIN_ERROR);
        }
        return ResultUtils.success(taskClaimService.claimTask(taskClaimForm,loginUser));
    }

    /**
     * 用户取消任务
     * @param taskId
     * @return
     */
    @PostMapping("/cancel/{taskId}")
    public BaseResponse<Boolean> cancelTask(@PathVariable Long taskId) {
        return ResultUtils.success(taskClaimService.cancelTask(taskId));
    }


    @GetMapping("/{userId}")
    public BaseResponse<List<TaskClaimVO>> userClaimedTask(@PathVariable Long userId) {
        return ResultUtils.success(taskClaimService.userClaimedTask(userId));
    }

    /**
     * 分页查询任务
     * @param taskForm
     * @return
     */
    @GetMapping("/page")
    public BaseResponse<Page<TaskClaimVO>> pageTask(TaskForm taskForm) {
        log.info("pageTask:{}",taskForm);
        return ResultUtils.success(taskClaimService.pageTask(taskForm));
    }

    //region

    /**
     * 分页查询任务
     * @param taskForm
     * @return
     */
    @GetMapping("/admin/page")
    public BaseResponse<Page<TaskClaimVO>> pageAdminTask(TaskForm taskForm) {
        return ResultUtils.success(taskClaimService.pageAdminTask(taskForm));
    }

    @PostMapping("/admin/pass/{id}")
    public BaseResponse<Boolean> passTaskClaim(@PathVariable Long id) {
        return ResultUtils.success(taskClaimService.passTaskClaim(id));
    }

    @PostMapping("/admin/reject/{id}")
    public BaseResponse<Boolean> rejectTaskClaim(@PathVariable Long id) {
        return ResultUtils.success(taskClaimService.rejectTaskClaim(id));
    }

    //endregion
}
